package com.aidex.biz.controller;

import com.aidex.biz.domain.LoanBillDetail;
import com.aidex.biz.service.LoanBillDetailService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 还款信息明细Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-07-03
 */
@RestController
@RequestMapping("/biz/bizLoanBillDetail")
public class LoanBillDetailController extends BaseController {
    @Autowired
    private LoanBillDetailService loanBillDetailService;

    /**
     * 查询还款信息明细列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizLoanBillDetail:list')")
    @GetMapping("/list")
    public R<PageInfo> list(LoanBillDetail loanBillDetail, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            loanBillDetail.setCompanyId(null);
        } else {
            loanBillDetail.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        loanBillDetail.setPage(new PageDomain(request, response));
        return R.data(loanBillDetailService.findPage(loanBillDetail));
    }

    /**
     * 获取还款信息明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizLoanBillDetail:query')")
    @GetMapping(value = "/{id}")
    public R<LoanBillDetail> detail(@PathVariable("id") String id) {
        return R.data(loanBillDetailService.get(id));
    }

    /**
     * 新增还款信息明细
     */
    @PreAuthorize("@ss.hasPermi('biz:bizLoanBillDetail:add')")
    @Log(title = "还款信息明细", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated LoanBillDetail loanBillDetail) {
        return R.status(loanBillDetailService.save(loanBillDetail));
    }

    /**
     * 修改还款信息明细
     */
    @PreAuthorize("@ss.hasPermi('biz:bizLoanBillDetail:edit')")
    @Log(title = "还款信息明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated LoanBillDetail loanBillDetail) {
        return R.status(loanBillDetailService.save(loanBillDetail));
    }


    /**
     * 删除还款信息明细
     */
    @PreAuthorize("@ss.hasPermi('biz:bizLoanBillDetail:remove')")
    @Log(title = "还款信息明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(loanBillDetailService.deleteLoanBillDetailByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizLoanBillDetail:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(loanBillDetailService.findMaxSort(new LoanBillDetail()));
    }

    /**
     * 导出还款信息明细列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizLoanBillDetail:export')")
    @Log(title = "还款信息明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(LoanBillDetail loanBillDetail) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            loanBillDetail.setCompanyId(null);
        } else {
            loanBillDetail.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<LoanBillDetail> list = loanBillDetailService.findList(loanBillDetail);
        ExcelUtil<LoanBillDetail> util = new ExcelUtil<LoanBillDetail>(LoanBillDetail.class);
        return util.exportExcel(list, "还款信息明细数据");
    }

    /**
     * 修改逾期状态
     */
    @Log(title = "修改逾期状态", businessType = BusinessType.INSERT)
    @PostMapping("/updateLoanBillDetailOverDue")
    public R updateLoanBillDetailOverDue(@RequestBody @Validated LoanBillDetail loanBillDetail) {
        return R.status(loanBillDetailService.updateLoanBillDetailOverDue(loanBillDetail));
    }

}
